import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {
  MatCell,
  MatCellDef,
  MatColumnDef,
  MatHeaderCell,
  MatHeaderCellDef,
  MatHeaderRow, MatHeaderRowDef, MatRow, MatRowDef,
  MatTable, MatTableDataSource
} from "@angular/material/table";
import {Observable, of} from "rxjs";
import {InstanceService} from "../../services/instance.service";
import {AsyncPipe, DatePipe, NgIf} from "@angular/common";
import {MatButton, MatIconButton} from "@angular/material/button";
import {MatIcon} from "@angular/material/icon";
import {Instance, InstanceCreate, InstanceUpdate} from "../../models/instance";
import {MatPaginator} from "@angular/material/paginator";
import {MatSort, MatSortHeader} from "@angular/material/sort";
import {MatFormField, MatLabel} from "@angular/material/form-field";
import {MatInput} from "@angular/material/input";
import {MatDialog, MatDialogModule} from "@angular/material/dialog";
import {InstanceCreateComponent} from "../instance-create/instance-create.component";
import {InstanceUpdateComponent} from "../instance-update/instance-update.component";
import {MatSnackBar} from "@angular/material/snack-bar";
import {MatMenu, MatMenuItem, MatMenuTrigger} from "@angular/material/menu";
import {MatDivider} from "@angular/material/divider";

@Component({
  selector: 'app-instance',
  standalone: true,
  imports: [
    MatTable,
    MatColumnDef,
    MatHeaderCell,
    MatHeaderCellDef,
    MatCell,
    MatCellDef,
    AsyncPipe,
    NgIf,
    MatHeaderRow,
    MatRow,
    MatHeaderRowDef,
    MatRowDef,
    MatButton,
    MatIconButton,
    MatIcon,
    MatPaginator,
    MatSort,
    MatSortHeader,
    MatFormField,
    MatInput,
    MatLabel,
    DatePipe,
    MatMenuTrigger,
    MatMenu,
    MatMenuItem,
    MatDivider,
  ],
  templateUrl: './instance.component.html',
  styleUrl: './instance.component.scss'
})
export class InstanceComponent implements OnInit, AfterViewInit {
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;
  dataSource: MatTableDataSource<Instance>;
  displayedColumns = ['instance_name', 'instance_describe', 'address', 'port', 'username','cnf_file', 'data_dir','status', 'create_at', '_tools'];

  constructor(private instanceService: InstanceService,
              private _snackBar: MatSnackBar,
              private dialog: MatDialog) {
    this.dataSource = new MatTableDataSource<Instance>();

  }

  ngOnInit(): void {
    this.listInstance();
  }

  instanceStatus(status: number): string{
    switch (status){
      case 0:
        return '未初始化';
      case 1:
        return '初始化中';
      case 2:
        return '已初始化';
      case 3:
        return '运行中';
      case 4:
        return '已下线';
      default:
        return `未知状态:${status}`;
    }
  }


  applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSource.filter = filterValue.trim().toLowerCase();

    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

  createInstance() {
    const dialogRef = this.dialog.open(InstanceCreateComponent);
    dialogRef.afterClosed().subscribe((result: InstanceCreate) => {
      if (result){
        this.instanceService.createInstance(result).subscribe(v => {
          this.listInstance();
        });
      }
    })
  }

  deleteInstance(id: string) {
    this.instanceService.deleteInstance(id).subscribe(result => {
      this.listInstance();
      this.openSnackBar(result, 'Success');
    })
  }

  updateInstance(data: Instance) {
    const dialogRef = this.dialog.open(InstanceUpdateComponent,
      {data: data});
    dialogRef.afterClosed().subscribe((result: InstanceUpdate) => {
      if(result){
        this.instanceService.updateInstance(result).subscribe(v => {
          this.listInstance();
        });
      }
    })
  }
  listInstance() {
    this.instanceService.listInstance().subscribe(v => {
      this.dataSource.data = v;
    });
  }

  ngAfterViewInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  openSnackBar(message: string, action: string){
    this._snackBar.open(message, action, {
      horizontalPosition: 'right',
      verticalPosition: 'top',
      duration: 3 * 1000
    });
  }

}
